package cn.tedu.service;

import cn.tedu.mapper.UserInfoMapper;
import cn.tedu.mapper.UserMapper;
import cn.tedu.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    public List<User> findAll() {
        return userMapper.findAll();
    }

    public void changeState(int state, String[] userIds) {
        userMapper.changeState(state, userIds);
    }

    public void delete(String[] userIds) {
        userMapper.delete(userIds);
        userInfoMapper.delete(userIds);
    }

    public void saveUser(User user) {
        //生产用户的id
        String userId = UUID.randomUUID().toString();
        user.setUserId(userId);
        user.getUserInfo().setUserInfoId(userId);


        // TODO Auto-generated method stub
        userMapper.saveUser(user);
        userInfoMapper.saveUser(user.getUserInfo());
    }

    public User findOneById(String userId) {
        // TODO Auto-generated method stub
        return userMapper.findOneById(userId);
    }
    public void update(User user) {
        //因为提交过来的数据user里面有id userinfo里面没有id
        user.getUserInfo().setUserInfoId(user.getUserId());

        userMapper.update(user);

        userInfoMapper.update(user.getUserInfo());
    }

    public void saveUserRole(String userId, String[] roleIds) {
        //清除之前的角色信息，然后重新添加
        userMapper.deleteRolesByUserId(userId);

        //添加角色的信息
        for (String roleId: roleIds
             ) {
            //执行添加操作
            userMapper.saveUserRole(userId,roleId);
        }
    }


}